const Koa = require('koa');
const Router = require('koa-router');
const Static = require('koa-static');
const cors = require('koa2-cors');
const path = require('path');

const api = require('./api');

const app = new Koa();
const router = new Router();

//跨域
app.use(cors({
    origin: function (ctx) {
        return "*"; // 允许来自所有域名请求
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));

//路由表
app.use(router.routes()).use(router.allowedMethods());

router.use('/api/elm/good',api.good.routes(),api.good.allowedMethods());
router.use('/api/elm/user',api.user.routes(),api.user.allowedMethods());
router.use('/api/elm/seller',api.seller.routes(),api.seller.allowedMethods());

//黑洞路由
app.use(Static(path.join(__dirname,'./static/')));

//端口监听
app.listen(80);